package solutions.leetcode.normal;

import solutions.lib.BaseSolution;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/reconstruct-original-digits-from-english/">423. 从英文中重建数字</a>
 */
public class Solution423 extends BaseSolution {
    enum Nums {
        zero(0, 'z', 'e', 'r', 'o'),        // z
        one(1, 'o', 'n', 'e'),                  // o
        two(2, 'w', 't', 'o'),              // w
        three(3, 'r', 'h', 't', 'e', 'e'),      // r
        four(4, 'u', 'o', 'f', 'r'),        // u
        five(5, 'f', 'i', 'v', 'e'),            // f
        six(6, 'x', 'i', 's'),              // x
        seven(7, 'v', 'e', 's', 'e', 'n'),          // v
        eight(8, 'g', 'i', 'e', 'h', 't'),  // g
        nine(9, 'i', 'n', 'n', 'e');                    // i

        final int num;
        final char[] chars;
        final char key;

        Nums(int num, char key, char... name) {
            this.key = key;
            this.num = num;
            this.chars = name;
        }
    }

    public String originalDigits(String s) {
        int[] times = new int[26];
        for (char c : s.toCharArray()) {
            times[c - 'a']++;
        }
        int[] rstTimes = new int[10];
        rstTimes[0] = times['z' - 'a'];
        rstTimes[2] = times['w' - 'a'];
        rstTimes[4] = times['u' - 'a'];
        rstTimes[6] = times['x' - 'a'];
        rstTimes[8] = times['g' - 'a'];

        rstTimes[1] = times['o' - 'a'] - rstTimes[4] - rstTimes[0] - rstTimes[2];
        rstTimes[5] = times['f' - 'a'] - rstTimes[4];
        rstTimes[3] = times['r' - 'a'] - rstTimes[0] - rstTimes[4];

        rstTimes[7] = times['v' - 'a'] - rstTimes[5];
        rstTimes[9] = times['i' - 'a'] - rstTimes[5] - rstTimes[6] - rstTimes[8];

        StringBuilder sb = new StringBuilder();
        for (int i = 0, size = rstTimes.length ; i < size ; i++) {
            int t = rstTimes[i];
            while (t-- > 0) {
                sb.append(i);
            }
        }
        return sb.toString();
    }

    private void check(Nums num, int[] val, int[] rstTimes) {
        int rst = val[num.key - 'a'];
        val[num.key - 'a'] = 0;
        for (char c : num.chars) {
            val[c - 'a'] -= rst;
        }
        rstTimes[num.num] = rst;
    }

    public static void main(String[] args) {
        Solution423 solution = new Solution423();
    }
}